<?php
/**
 *  消息推送
 */
namespace Clientside\Controller;

use Common\Controller\AdminbaseController;
use Think\Exception;

class AdminMesspushController extends AdminbaseController {

    protected $mess_model;
    protected $push_model;
    protected $taxonomys=array("activity"=>"活动","product"=>"商品","news"=>"资讯");

    function _initialize() {
        parent::_initialize();
        $this->mess_model = D("PushMess");
        $this->push_model = D("Push");
        $this->assign("taxonomys",$this->taxonomys);
    }

    /**
     * 推送信息列表
     * @return [type] [description]
     */
    public function index()
    {

        $where['mess_status']=array(array('neq','-1'));

        $this->mess_model
        ->alias("a")
        ->where($where);

        $count=$this->mess_model->count();

        $page = $this->page($count, 20);


        $this->mess_model
        ->alias("a")
        // ->join("__USERS__ c ON a.uid = c.id")
        ->where($where)
        ->limit($page->firstRow , $page->listRows)
        ->order("a.create_time DESC");
        // $this->mess_model->field('a.*,c.user_login,c.user_nicename');
        $posts=$this->mess_model->select();


        $this->assign("page", $page->show('Admin'));
        $this->assign("formget",array_merge($_GET,$_POST));
        $this->assign("posts",$posts);

        $this->display();
    }

    public function delete() {
        $id = I("get.id",0,'intval');

        if ($this->mess_model->delete($id)!==false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }

    /**
     * push消息
     * @return [type] [description]
     */
    public function push_mess()
    {

    }

    /**
     * push设置
     * @return [type] [description]
     */
    public function push()
    {
        $post = $this->push_model->find();
        $this->assign('post',$post);
        $this->display();
    }

    /**
     * push设置提交
     * @return [type] [description]
     */
    public function push_post(){
        if (IS_POST) {
            $post_id=intval($_POST['post']['id']);

            $_POST['post']['update_time']=date("Y-m-d H:i:s",time());
            $push=I("post.post");
            $push['sign_description']=htmlspecialchars_decode($push['sign_description']);
            $result=$this->push_model->where(['id'=>$post_id])->save($push);
            if ($result!==false) {
                $this->success("保存成功！",U("AdminMesspush/index"));
            } else {
                $this->error("保存失败！");
            }
        }
    }
}